public class LeetCode151 {
    public static void main(String[] args) {
        LeetCode151 leetCode151 = new LeetCode151();
        System.out.println(leetCode151.reverseWords("a good   example"));
    }

    public String reverseWords(String s) {
        String[] array = s.trim().split(" ");
        int startIndex = 0;
        int endIndex = array.length - 1;
        while (startIndex < endIndex){
            String temp = array[startIndex];
            array[startIndex] = array[endIndex];
            array[endIndex] = temp;
            startIndex++;
            endIndex--;
        }

        StringBuffer buffer = new StringBuffer();
        for(String item:array){
            if (item.trim().equals("")){
                continue;
            }
            buffer.append(item).append(" ");
        }

        return buffer.toString().trim();
    }
}
